<?php

class ExhibitionsController extends Controller
{
	public $layout='//layouts/admin/main';

	public function init(){
        Yii::app()->user->loginUrl = array('/admin/site/login');
        parent::init();
    }

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'new', 'edit', 'delete', 'accept', 'decline'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

  public function actionAccept(){
    $exhibition = Exhibitions::model()->find('id = :id', array(':id' => $_GET['id']));
    $exhibition->status = 1;
    $exhibition->modified_at = date('Y-m-d H:i:s');
    $exhibition->save();
    
    Yii::app()->user->setFlash('success', 'Success');
    Yii::app()->user->setFlash('content', 'You have successfully finished the exhibition.');

    if(isset($_GET['page'])){
      $this->redirect(bu('admin/exhibitions/'.$exhibition->id));
    }else{
      $this->redirect(bu('admin/exhibitions'));
    }
  }

  public function actionDecline(){
    $exhibition = Exhibitions::model()->find('id = :id', array(':id' => $_GET['id']));
    $exhibition->status = 0;
    $exhibition->modified_at = date('Y-m-d H:i:s');
    $exhibition->save();
    
    Yii::app()->user->setFlash('success', 'Success');
    Yii::app()->user->setFlash('content', 'You have denied the exhibition.');

    if(isset($_GET['page'])){
      $this->redirect(bu('admin/exhibitions/'.$exhibition->id));
    }else{
      $this->redirect(bu('admin/exhibitions'));
    }
  }

	public function actionDelete()
	{
		$exhibition=Exhibitions::model()->find('id = :id', array(':id'=>$_GET['id']));
		$exhibition->delete();

    Yii::app()->user->setFlash('success', 'Success');
    Yii::app()->user->setFlash('content', 'You have successfully delete an exhibition.');
		$this->redirect(bu('admin/exhibitions/'));
	}

	public function actionIndex()
	{
    $criteria = new CDbCriteria;
    $criteria->order = "created_at DESC";

		$exhibitions=Exhibitions::model()->findAll($criteria);
    $count = count($exhibitions);

    //Pagination
    $pages = new CPagination($count);
    $pages -> pageSize = 10;
    $pages -> applyLimit($criteria);

    $exhibitions=Exhibitions::model()->findAll($criteria);

		$this->render('index',array(
			'exhibitions' => $exhibitions,
      'pages' => $pages,
		));
	}
  
  public function actionView($id)
	{
		$exhibition=Exhibitions::model()->find('id = :id', array(':id' => $id));

		$this->render('view',array(
			'exhibition' => $exhibition,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=News::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='news-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
